package cn.net.susan.service.common;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import cn.net.susan.exception.BusinessException;
import cn.net.susan.service.BaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.susan.mapper.common.CommonJobMapper;
import cn.net.susan.entity.common.CommonJobConditionEntity;
import cn.net.susan.entity.common.CommonJobEntity;
import cn.net.susan.entity.ResponsePageEntity;
import cn.net.susan.util.AssertUtil;
import cn.net.susan.util.FillUserUtil;
import cn.net.susan.mapper.BaseMapper;
/**
 * 定时任务 服务层
 *
 * @author 苏三 该项目是知识星球：java突击队 的内部项目
 * @date 2024-04-30 15:09:06
 */
@Service
public class CommonJobService extends BaseService< CommonJobEntity,  CommonJobConditionEntity> {

	@Autowired
	private CommonJobMapper commonJobMapper;

	/**
     * 查询定时任务信息
     *
     * @param id 定时任务ID
     * @return 定时任务信息
     */
	public CommonJobEntity findById(Long id) {
	    return commonJobMapper.findById(id);
	}

	/**
     * 根据条件分页查询定时任务列表
     *
     * @param commonJobConditionEntity 定时任务信息
     * @return 定时任务集合
     */
	public ResponsePageEntity<CommonJobEntity> searchByPage(CommonJobConditionEntity commonJobConditionEntity) {
		return super.searchByPage(commonJobConditionEntity);
	}

    /**
     * 新增定时任务
     *
     * @param commonJobEntity 定时任务信息
     * @return 结果
     */
	public int insert(CommonJobEntity commonJobEntity) {
		checkParam(commonJobEntity);
		commonJobEntity.setPauseStatus(false);
		return commonJobMapper.insert(commonJobEntity);
	}

	/**
     * 修改定时任务
     *
     * @param commonJobEntity 定时任务信息
     * @return 结果
     */
	public int update(CommonJobEntity commonJobEntity) {
		AssertUtil.notNull(commonJobEntity.getId(),"id不能为空");
		checkParam(commonJobEntity);
		return commonJobMapper.update(commonJobEntity);
	}

	private void checkParam(CommonJobEntity commonJobEntity) {
		commonJobEntity.setBeanName(commonJobEntity.getBeanName().trim());

		CommonJobConditionEntity commonJobConditionEntity = new CommonJobConditionEntity();
		commonJobConditionEntity.setBeanName(commonJobEntity.getBeanName());
		List<CommonJobEntity> commonJobEntities = commonJobMapper.searchByCondition(commonJobConditionEntity);
		if (Objects.nonNull(commonJobEntity.getId())) {
			Optional<CommonJobEntity> optional = commonJobEntities.stream()
					.filter(x -> x.getId().equals(commonJobEntity.getId())).findAny();
			if(optional.isPresent()) {
				throw new BusinessException("该定时任务已存在，请重新修改");
			}
		} else {
			if (CollectionUtils.isNotEmpty(commonJobEntities)) {
				throw new BusinessException("该定时任务已存在，请勿重复添加");
			}
		}
	}

	/**
     * 批量删除定时任务对象
     *
     * @param ids 系统ID集合
     * @return 结果
     */
	public int deleteByIds(List<Long> ids) {
		List<CommonJobEntity> entities = commonJobMapper.findByIds(ids);
		AssertUtil.notEmpty(entities, "定时任务已被删除");

		CommonJobEntity entity = new CommonJobEntity();
		FillUserUtil.fillUpdateUserInfo(entity);
		return commonJobMapper.deleteByIds(ids, entity);
	}

	@Override
	protected BaseMapper getBaseMapper() {
		return commonJobMapper;
	}

}
